<?php

namespace App\Http\Controllers\SiteAce;

use App\Http\Controllers\Controller;
use App\Services\Form\initFormData as InitFormData;
use Illuminate\Http\Request;

class Twnews extends Controller
{
    use \App\Traits\CacheTrait;
    public $request;
    public $formData;
    public $breadMenuList;
    use \App\Traits\ApiTrait;
    public $curl;

    public function __construct(Request $request, InitFormData $formData)
    {
        $this->request = $request;
        $this->formData = $formData;
        $this->breadMenuList = config('front.bread_menu');
        $this->curl = new \Ixudra\Curl\CurlService();
        $this->tag=config('seoAce.twNews');
    }

    public function index()
    {
      $page=$this->request->page;
      if(!isset($page))$page=1;
      $pageSize=3;
      $idArr=[
        '0'=>'20',
        '1'=>'20/24',
        '2'=>'20/22',
        '3'=>'20/23',
        '4'=>'20/21',
        '5'=>'54',
        '6'=>'58',
      ];
      if(isset($_REQUEST['cid']) && $_REQUEST['cid']>0 && $_REQUEST['cid']<=6)
      $cid=$_REQUEST['cid'];

      else {
        $cid=0;
      }
      $categoryId=$idArr["$cid"];
      $list=$this->ajaxArticleList($categoryId,$page,$pageSize);
      // print_r($list);
       $total=$list['data']['total'];
      if($list['code']!=-1){
        $listData=$list['data']['records'];
      }
      else $listData=[];
      $viewData = [
          'seo' => $this->returnSeoInfo('market_index'),
          'selectedMenu' => parent::MENU_MARKET_COO,
          'listData'=>$listData,
          'tag'=>$this->tag['tag'],
          'cid'=>$cid,
          'total'=>$total,
          'page'=>$page,
          'pageSize'=>$pageSize,
      ];
      switch ($cid) {
        case '1':
          return view('webAce.twnews.index', $viewData);
          break;
        case '2':
          return view('webAce.twnews.index', $viewData);
          break;
        case '3':
          return view('webAce.twnews.index', $viewData);
          break;
        case '4':
          return view('webAce.twnews.index', $viewData);
          break;
        case '6':
          return view('webAce.twnews.index_link', $viewData);
          break;
        default:
          return view('webAce.twnews.index', $viewData);
          break;
      }
    }

    public function ajaxArticleList($id,$pageNumber,$pageSize=5){
      // $pageNumber=1;
      // $pageSize=10;
      // print_r($pageNumber.'-'.$pageSize);
      $categoryId=$id;
      // $pageNumber=$this->msgPageNum;
      //验证是否有cookie信息
        $rqUrl='http://api.crm.tanwin.cn/article/findByProgramaId';
        $rqData=['article_programa_id'=>$categoryId,'pageNumber'=>$pageNumber,'pageSize'=>$pageSize];
        $result=$this->curl->to($rqUrl)
        ->withOption('TIMEOUT', 10)
        ->withData($rqData)
        ->post();
        // print_r('请求地址是'.$rqUrl.'</br>');
        // print_r('请求结果是'.$result);
        try {
            $result=json_decode($result,true);
            if($result['code']==100){
              $rData=$result['data'];
              $data['code']=1;
              $data['data']=$rData;
              return $data;
            }
          } catch (Exception $e) {
            $data['data']=null;
            $data['code']=-1;
            return $data;
          }
    }

}
